cmake_minimum_required(VERSION 3.15)

project(waylandpp-demo)

find_package(PkgConfig REQUIRED)

pkg_check_modules(WAYLAND_CLIENT REQUIRED wayland-client++)
find_program(WAYLAND_SCANNER wayland-scanner++)

pkg_check_modules(DRM REQUIRED libdrm)

pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVFORMAT REQUIRED libavformat)
pkg_check_modules(AVUTIL REQUIRED libavutil)
pkg_check_modules(SWSCALE REQUIRED libswscale)

add_custom_command(
    OUTPUT wlr-screencopy.cpp
    COMMAND ${WAYLAND_SCANNER} ${CMAKE_SOURCE_DIR}/wlr-screencopy-unstable-v1.xml ${CMAKE_CURRENT_BINARY_DIR}/wlr-screencopy.h ${CMAKE_CURRENT_BINARY_DIR}/wlr-screencopy.cpp
    DEPENDS ${CMAKE_SOURCE_DIR}/wlr-screencopy-unstable-v1.xml
)

add_custom_command(
    OUTPUT linux-dmabuf.cpp
    COMMAND ${WAYLAND_SCANNER} ${CMAKE_SOURCE_DIR}/linux-dmabuf-unstable-v1.xml ${CMAKE_CURRENT_BINARY_DIR}/linux-dmabuf.h ${CMAKE_CURRENT_BINARY_DIR}/linux-dmabuf.cpp
    DEPENDS ${CMAKE_SOURCE_DIR}/linux-dmabuf-unstable-v1.xml
)

include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}

    ${GBM_INCLUDE_DIRS}
    ${DRM_INCLUDE_DIRS}
    ${AVCODEC_INCLUDE_DIRS}
    ${AVFORMAT_INCLUDE_DIRS}
    ${AVUTIL_INCLUDE_DIRS}
    ${SWSCALE_INCLUDE_DIRS}
)

add_executable(${PROJECT_NAME}
    linux-dmabuf.cpp
    main.cpp
    wlr-screencopy.cpp
)

target_link_libraries(${PROJECT_NAME}
    wayland-client++
    drm
    gbm
    ${AVCODEC_LIBRARIES}
    ${AVFORMAT_LIBRARIES}
    ${AVUTIL_LIBRARIES}
    ${SWSCALE_LIBRARIES}
)

# Debug Build Only
if (CMAKE_BUILD_TYPE STREQUAL "Debug")

    find_program(CLANG_FORMAT clang-format)
    if (CLANG_FORMAT)
        if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks/pre-commit)
            file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pre-commit
                DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/.git/hooks
                FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
            )
        endif()
    else()
        message(FATAL_ERROR "clang-format not found! It is required to format code.")
    endif()
endif()